//
//  XYNewFeatureView.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/11.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

class XYNewFeatureView: UIScrollView {
    
    //进入微博的闭包
    var inputBlock:(()->())?
    //分享到微博的闭包
    var seletBlock:((seletedButton:UIButton)->())?
    
    //重写initwithFrame方法来初始化自己的view
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置分页效果
        self.pagingEnabled = true
        //水平下划线
        self.showsHorizontalScrollIndicator = false
        //弹簧效果
        self.bounces = false
        
    }

    
    //自定义一个方法来初始化自己的界面
    func loadScrollView(imageName:String,imageCount:Int){
        
        
        for i in 0..<imageCount{
            //初始化imageView
            let imageView = UIImageView(image: UIImage(named: "\(imageName)_\(i+1)"))
            //给ImageView设置属性
            //用户交互
            imageView.userInteractionEnabled = true
            //大小
            imageView.size = self.size
            //x的坐标
            imageView.x = SCREENW * CGFloat(i)
            
            //给最后一张图片添加一个按钮
            if i == (imageCount-1){
                
                loadImageViewButton(imageView)
                
            }
            //把imageView添加到scrollView上
            self.addSubview(imageView)
        }
        //设置scroll的移动范围
        self.contentSize = CGSizeMake(CGFloat(imageCount) * SCREENW, 0)

    }
    
    
    private func  loadImageViewButton(imageView:UIImageView){
        

        //创建进入微博的button
        let inputButton = UIButton()
        inputButton.addTarget(self, action: "inputWeiBo", forControlEvents: UIControlEvents.TouchUpInside)
        //设置图片
        inputButton.setTitle("进入微博", forState: UIControlState.Normal)
        inputButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        inputButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置按钮的位置和大小
        inputButton.sizeToFit()
        inputButton.centerX = self.centerX
        inputButton.y = SCREENH * 0.8
        //添加到imageView
        imageView.addSubview(inputButton)
        
        //添加选中按钮
        let seleButton = UIButton()
        seleButton.addTarget(self, action: "didSeleButton:", forControlEvents: UIControlEvents.TouchUpInside)
        //设置图片和title
        seleButton.setTitle("分享到微博", forState: UIControlState.Normal)
        seleButton.setTitleColor(ColorDefine(r: 80, g: 80, b: 80), forState: UIControlState.Normal)
        seleButton.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        seleButton.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        
        //设置按钮的位置和大小
        seleButton.sizeToFit()
        seleButton.centerX = self.centerX
        seleButton.y = inputButton.y - 50
        imageView.addSubview(seleButton)
    }
    //进入微博的点击事件
    @objc private func inputWeiBo(){
        if let input = self.inputBlock{
            input()
        }
    }
    
    //分享到微博的点击事件
    @objc private func didSeleButton(seleButton:UIButton){
        if let seleted = self.seletBlock{
            seleted(seletedButton: seleButton)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
